#coding:utf-8
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
import json
#from common import httpclient as http
import common.httpclient as http
import config.myurls as myurls

import time
import datetime
import pytz



def index(req):
  '首页'

  id=0
  try:
    userinfo=req.session['userinfo']
    id=userinfo['id']
  except KeyError:
    pass
  if id==0:
    return HttpResponseRedirect('/login')
  
  main_nav=[]
  main_nav.append({"url":"/persion","name":"个人中心"})
  
  context={}
  context['title']=''
  context['main_nav']=main_nav
  return render(req, 'index.html', context)


def login(req):
  '登录页'
  try:
    del req.session['userinfo']
  except KeyError:
    pass
  main_nav=[]
  main_nav.append({"url":"","name":"登录页面"})
  context={}
  context['title'] = '-登录页面'
  context['main_nav']=main_nav
  return render(req, 'login.html', context)
  
def ckpwd(req):
  '校验密码'
  #if req.method=='POST':
  
  url=myurls.ApiAdminuserLogin
  
  param=[];
  param.append(('username',req.POST.get("username")))
  param.append(('pwd',http.md5(req.POST.get("pwd"))))
  
  rt=[]
  obj=[]
  try:
    rt=http.getDateByUrl(url,param)
    obj=json.loads(rt);
  except ValueError:
    pass
  
  if obj['response_code']==1:
    req.session['userinfo']=obj['userinfo']
    #return HttpResponseRedirect('/')
  
  return HttpResponse(rt,content_type="application/json")
  #return HttpResponse(u"校验密码:"+str(obj));
  #return HttpResponseRedirect('/index/')
    

